package me.mingshan.leetcode;

/**
 * @author hanjuntao
 * @date 2025/10/1 0001
 */
public class L_100_相同的树 {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode node1 = new TreeNode(2);
        TreeNode node2 = new TreeNode(2);
        TreeNode roo2 = new TreeNode(1);
        TreeNode node4 = new TreeNode(2);
        TreeNode node5 = new TreeNode(2);
        root.left = node1;
        root.right = node2;

        roo2.left = node4;
        roo2.right = node5;

        System.out.println(isSameTree(root, roo2));
    }

    /**
     * 递归实现
     *
     * 1. 比较当前节点的值, 如果不相等，则返回false
     * 2. 递归判断左右子树
     *
     * @param p
     * @param q
     * @return
     */
    public static boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }

        // 有一个为空，则返回false
        if (p == null || q == null) {
            return false;
        }

        if (p.val != q.val) {
            return false;
        }

        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}
